import 'package:flutter/material.dart';
import 'package:flutter_bank/addCard.dart';
import 'package:flutter_bank/allCards.dart';
import 'package:flutter_bank/colors.dart';
import 'package:flutter_bank/homePage.dart';
import 'package:flutter_bank/mePage.dart';
import 'package:flutter_bank/statisticsPage.dart';

late double scrW, scrH;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final PageController pageController = PageController();
  int currentIndex = 0;

  void onIndexChanged(int index) {
    setState(() {
      currentIndex = index;
    });
    pageController.jumpToPage(index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // PageController 控制 PageView 呈现页面
      body: PageView(
        // physics: const NeverScrollableScrollPhysics(),
        controller: pageController,
        // onPageChanged: onIndexChanged,
        children: const [
          HomePage(),
          StatisticsPage(),
          AllCards(),
          MePage(),
        ],
      ),
      // 底部带凹下的导航
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        color: Colors.white,
        notchMargin: 10,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconButton(
              icon: Image.asset(
                'images/home/Icon__Card.png',
                width: 25,
                height: 25,
              ),
              onPressed: () {
                onIndexChanged(0);
              },
            ),
            IconButton(
              icon: Image.asset(
                'images/home/Icon__Card (1).png',
                width: 25,
                height: 25,
              ),
              onPressed: () {
                onIndexChanged(1);
              },
            ),
            SizedBox(), // 添加一个空的占位符来保持布局的对齐
            IconButton(
              icon: Image.asset(
                'images/home/Iconly_Light-outline_Wallet.png',
                width: 25,
                height: 25,
              ),
              onPressed: () {
                onIndexChanged(2);
              },
            ),
            IconButton(
              icon: Image.asset(
                'images/home/Iconly_Bold_Profile.png',
                width: 25,
                height: 25,
              ),
              onPressed: () {
                onIndexChanged(3);
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (currentIndex == 2) {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return AddCard();
              }));
            }
          },
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(40.0),
          ),
          backgroundColor: Colors.white,
          child: Center(
              child: currentIndex == 2
                  ? Container(
                width: 50,
                height: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(100),
                  color: AppColor.bottomColor,
                ),
                    child: Center(
                      child: Icon(
                          Icons.add,
                          size: 50,
                          color: Colors.white,
                        ),
                    ),
                  )
                  : Container(
                    width: 50,
                    height: 50,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      color: AppColor.bottomColor,
                    ),
                    child: Center(
                      child: Image.asset(
                          'images/home/Shape.png',
                          width: 24,
                        ),
                    ),
                  ))), // 浮动按钮
      floatingActionButtonLocation:
          FloatingActionButtonLocation.centerDocked, // 浮动按钮 停靠在底部中间位置
    );
  }
}
